/* eslint-disable import/no-anonymous-default-export */
import { Dispatch } from 'redux'

// 定义State类型
type State = Readonly<{
  employeeList: any[] | undefined
}>

// 定义Action类型
type Action = {
  type: string,
  payload: any
}

const initState:State = {
  employeeList: undefined
}
// 定义dispatch
export function getEmployee(param: any, callBack:() => void) {
  // redux-thunk
  return (dispatch:Dispatch) => {
    // 模拟做axios请求
    console.log('param', param)
    new Promise((resolve) => {
      setTimeout(() => {
        const mock = {
          data:[
            {
              name:'小明',
              age:18
            },
            {
              name:'小辉',
              age:16
            }
          ]
        }
        resolve(mock)
      }, 2000)
    })
    .then((res: any) => {
      console.log('res', res)
      dispatch({
        type: "GET_EMPLOYEE",
        payload: res.data
      })
      typeof callBack === "function" && callBack()
    })
  }
}

export default function(state = initState, action:Action) {
  switch (action.type) {
    case "GET_EMPLOYEE":
      return {
        ...state,
        employeeList: action.payload
      }
    default:
        return state
  }
}
